package com.goods.business.controller;

import com.goods.business.service.OutStockService;
import com.goods.common.error.SystemException;
import com.goods.common.response.ResponseBean;
import com.goods.common.vo.business.OutStockDetailVO;
import com.goods.common.vo.business.OutStockVO;
import com.goods.common.vo.system.PageVO;
import com.goods.common.vo.system.UserInfoVO;
import com.goods.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author liangliang
 * @Date 2021/11/10 18:41 Administrator
 * @Description
 */
@RestController
@RequestMapping("/business/outStock")
public class OutStockController {
    @Autowired
    private OutStockService outStockService;
    @Autowired
    private UserService userService;

    /**
     * 分页查询物资发放记录
     * @param map
     * @return
     */
    @GetMapping("/findOutStockList")
    private ResponseBean findOutStockList(@RequestParam Map map){
        PageVO<OutStockVO> outStockVOPageVO=outStockService.findOutStockList(map);
        return ResponseBean.success(outStockVOPageVO);
    }

    /**
     * 查询物资的发放明细
     * @param id
     * @return
     */
    @GetMapping("/detail/{id}")
    public ResponseBean detail(@PathVariable Long id){
        OutStockDetailVO outStockDetailVO=outStockService.detail(id);
        return ResponseBean.success(outStockDetailVO);
    }

    /**
     * 将商品移入回收站
     * @param id
     * @return
     */
    @PutMapping("/remove/{id}")
    public ResponseBean remove(@PathVariable Long id){
        outStockService.remove(id);
        return ResponseBean.success();
    }


    /**
     * 将回收站中的数据还原
     * @param id
     * @return
     */
    @PutMapping("/back/{id}")
    public ResponseBean back(@PathVariable Long id){
        outStockService.back(id);
        return ResponseBean.success();
    }

    /**
     * 将回收站中的数据删除
     * @param id
     * @return
     */
    @PutMapping("/delete/{id}")
    public ResponseBean delete(@PathVariable Long id){
        outStockService.delete(id);
        return ResponseBean.success();
    }

    /**
     * 审核通过
     * @param id
     * @return
     */
    @PutMapping("/publish/{id}")
    public ResponseBean publish(@PathVariable Long id){
        outStockService.publish(id);
        return ResponseBean.success();
    }

    /**
     * 物资发放保存
     * @param outStockVO
     * @return
     */
    @PostMapping("/addOutStock")
    public ResponseBean addOutStock(@RequestBody OutStockVO outStockVO){
        try {
            UserInfoVO userInfoVO = userService.info();
            String username = userInfoVO.getUsername();
            outStockVO.setOperator(username);
            outStockService.addOutStock(outStockVO);
        } catch (SystemException e) {
            e.printStackTrace();
        }
        return ResponseBean.success();
    }
}
